package com.fingard.dsp.bank.directbank.gxnx02;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/10/10.
 */
public class GXNX02Balance extends GXNX02Base implements IBalance{

    /**
     * 实时余额查询
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        return d_7506_BalQryB(p_req7506);
    }

    public RetAts7506BalQry d_7506_BalQryB(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = null;
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                GXNX02Header header = new GXNX02Header();
                header.tranCode ="1001S";
                header.tranSeq = Format.DateTime14Format.format(new Date())+bankFront.operator
                        +header.tranCode.substring(0,4)+getQryTransID();
                writeCommonHeader1(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("REQBODY");
                xmlTextWriter.writeElementString("AcctNo",p_req7506.readedDetail.actNum);
                xmlTextWriter.writeElementString("OneFlg","1");
                xmlTextWriter.writeEndDocument();
                String[] bankResult = sendToBank1(xmlTextWriter.toXmlString());
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String rspCode = tmpBkReader.getFirstText("/GXNXYQ/RESP/RESPHEAD/RspCode");
                        String rspMsg = tmpBkReader.getFirstText("/GXNXYQ/RESP/RESPHEAD/RspMsg");
                        if ("000000".equals(rspCode)) {
                            if (tmpBkReader.read("/GXNXYQ/RESP/RESPBODY")){
                                detail = new RetAtsBalDetail();
                                detail.respCode = "0";
                                detail.respInfo = rspMsg;
                                detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CCY"));
                                detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("AcctNo");
                                detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvaBal");
                                detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("Bal");
                            }
                        } else {
                            detail = new RetAtsBalDetail();
                            detail.respCode = "-1";
                            detail.respInfo = rspMsg;
                        }
                    } else {
                        detail = new RetAtsBalDetail();
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                } else {
                    detail = new RetAtsBalDetail();
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
